package com.pshare.framework.query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import com.pshare.constant.SeaxConstants;
import com.pshare.util.StringUtils;

/**
 * 分页查询对象
 */
public class QueryInfo {
	
	private boolean highlight; // 是否高亮

	private Integer page = 1;

	private Integer pageSize = SeaxConstants.DEFAULT_PAGE_SIZE;

	public static final String DEFAULT_SORT_COLUMN = "create_time";

	public static final Direction DEFAULT_SORT_DIR = Direction.DESC;

	protected static Map<String, String> replaceColumns = new HashMap<String, String>();

	private Map<String, Object> params = new HashMap<String, Object>();

	private List<Sort> sorts = new ArrayList<Sort>();

	static {
		replaceColumns.put("ct", DEFAULT_SORT_COLUMN);
	}

	public enum ORDER {
		desc, asc;
		public ORDER reverse() {
			return (this == asc) ? desc : asc;
		}
	}

	public boolean isHighlight() {
		return highlight;
	}

	public void setHighlight(boolean highlight) {
		this.highlight = highlight;
	}

	/**
	 * 传参
	 * 
	 * @return Map<String, Object>
	 */
	public Map<String, Object> getParams() {
		return params;
	}

	public void addParams(String key, Object value) {
		this.params.put(key, value);
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		if (page != null)
			this.page = page;
	}

	public QueryInfo clearSorts() {
		sorts.clear();
		return this;
	}

	/**
	 * 替换排序字段
	 * 
	 * @param source
	 * @return
	 */
	public String replaceSortColumn(String source) {
		String replaced = replaceColumns.get(source);
		return replaced == null ? "" : replaced;
	}

	/**
	 * 手动添加排序字段，该方法专供service 使用
	 * 
	 * @param sortStr
	 */

	public void addSortColumn(String column, Direction sortDir) {
		if (StringUtils.isBlank(column)) {
			return;
		}
		if (sortDir == null) {
			sortDir = DEFAULT_SORT_DIR;
		}

		sorts.add(new Sort(sortDir, column));
	}

	/**
	 * 将排序字符串保存成排序对象
	 * 
	 * @param sortStr
	 */
	public void setSortStr(List<String> sortStr) {
		for (String str : sortStr) {
			addSortStr(str);
		}
	}

	public void addSortStr(String sortStr) {
		String[] item = sortStr.split(":");
		if (item.length == 0) {
			return;
		}
		String column = item[0];
		Direction dir = DEFAULT_SORT_DIR;
		if (item.length == 2) {
			dir = "asc".equalsIgnoreCase(item[1]) ? Direction.ASC : DEFAULT_SORT_DIR;
		}
		String replacedColumn = this.replaceSortColumn(column);
		if (StringUtils.isNotBlank(replacedColumn)) {
			Sort sort = new Sort(dir, replacedColumn);
			sorts.add(sort);
		}
	}

	public List<Sort> getSorts() {
		return sorts;
	}
	
	/**
	 * 拼接sql
	 * 
	 * @toosee MapUtil
	 * @return
	 */
	public String getOrderBySql() {
		String cs = "";
		for (Sort sort : sorts) {
			cs += sort.toString() + ",";
		}
		if (cs.length() > 1) {
			cs = cs.substring(0, cs.length() - 1).replaceAll(":", "");
		}
		return cs;
	}

}
